package ua.crimea.bat.ss.model.beans;

import org.springframework.beans.support.MutableSortDefinition;
import org.springframework.beans.support.PropertyComparator;
import org.hibernate.annotations.Fetch;
import org.hibernate.annotations.FetchMode;
import org.hibernate.annotations.LazyToOne;
import org.hibernate.annotations.LazyToOneOption;

import javax.persistence.*;
import java.util.*;

/**
 * User: bat
 * Date: 21.03.2010
 * Time: 14:34:05
 */
@Entity
@Table(name = "albums")

/*@SequenceGenerator(
        name = "SEQ_ID1",
        sequenceName = "album_seq1",
        allocationSize = 20)*/

public class Album extends BaseBean{

    private String name;

    private java.util.Date creationDate;

    private java.util.Date modifyDate;

    private java.util.Date publishDate;

    private Boolean isPublic;

    private Set<Image> images;

    //@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "SEQ_ID1")

    public Album() {
        super();
    }

    @Column(length = 100)
    @LazyToOne(LazyToOneOption.FALSE)
    @Fetch(FetchMode.SELECT)
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Column(name = "creation_date")
    @Temporal(TemporalType.TIMESTAMP)
    @LazyToOne(LazyToOneOption.FALSE)
    @Fetch(FetchMode.SELECT)
    public java.util.Date getCreationDate() {
        return creationDate;
    }

    public void setCreationDate(java.util.Date creationDate) {
        this.creationDate = creationDate;
    }

    @Column(name = "modify_date")
    @Temporal(TemporalType.TIMESTAMP)
    @LazyToOne(LazyToOneOption.FALSE)
    @Fetch(FetchMode.SELECT)
    public java.util.Date getModifyDate() {
        return modifyDate;
    }

    public void setModifyDate(java.util.Date modifyDate) {
        this.modifyDate = modifyDate;
    }

    @Column(name = "publish_date")
    @Temporal(TemporalType.TIMESTAMP)
    @LazyToOne(LazyToOneOption.FALSE)
    @Fetch(FetchMode.SELECT)
    public java.util.Date getPublishDate() {
        return publishDate;
    }

    public void setPublishDate(java.util.Date publishDate) {
        this.publishDate = publishDate;
    }

    @Column(name = "is_public")
    @Basic
    @LazyToOne(LazyToOneOption.FALSE)
    @Fetch(FetchMode.SELECT)
    public Boolean getIsPublic() {
        return isPublic;
    }

    public void setIsPublic(Boolean aPublic) {
        this.isPublic = aPublic;
    }

    protected void setImagesInternal(Set<Image> images) {
        this.images = images;
    }

    @OneToMany(targetEntity=Image.class, cascade = {javax.persistence.CascadeType.REMOVE},fetch = FetchType.EAGER, mappedBy = "album")
    protected Set<Image> getImagesInternal() {
        if (this.images == null) {
            this.images = new HashSet<Image>();
        }
        return this.images;
    }

    @Transient
    public List<Image> getImages() {
        List<Image> sortedPets = new ArrayList<Image>(getImagesInternal());
        PropertyComparator.sort(sortedPets, new MutableSortDefinition("name", true, true));
        return Collections.unmodifiableList(sortedPets);
    }

    public void addImage(Image image) {
        getImagesInternal().add(image);
        image.setAlbum(this);
    }

    @Transient
    public Image getImage(String name) {

        return getImage(name, false);
    }
    @Transient
    public Image getImage(String name, boolean ignoreNew) {
        name = name.toLowerCase();
        for (Image image : getImagesInternal()) {
            String compName = image.getName();
            if (compName.equalsIgnoreCase(name)) {
                return image;
            }
        }
        return null;
    }
}